var iframeName = (window.frameElement && window.frameElement.id) || "noIframe";
$(function () {
  // $("input").val("22");
  // $("input").html("22");
  $("#formSignin").ajaxForm(function (data) {
    // var tmp = $("#addForm").serializeArray(); //表单序列化 = > 直接将表单的数据内容提取为查询字符串格式(键 = 值)
    // console.log(data);
    // console.log(tmp)
    if (data.code == 200) {
      if (iframeName == "iframe4") {
        Toast("账户密码修改成功", "success", 500, 1000, 100);
        window.parent.narn("success", "修改成功-" + data.msg, "提示");
        setTimeout(function () {
          window.parent.nodeFrameget(); //关闭修改模态框窗口
        }, 1000);
      } else if (iframeName == "noIframe") {
        Toast("账户密码修改成功", "info", 500, 1000, 100);
        var plotal = window.location.protocol;
        var path = window.location.host;
        var herf = window.location.href;
        var address = plotal + "//" + path;
        // console.log(plotal + path);
        window.location.replace(address);
      }
    } else {
      Toast("账户密码修改失败-" + data.msg, "error", 500, 1000, 100);
      if (iframeName == "iframe4") {
        window.parent.narn("error", "修改失败-" + data.msg, "提示");
        setTimeout(function () {
          //   window.parent.nodeFrameget(); //关闭修改模态框窗口
        }, 1000);
      } else if (iframeName == "noIframe") {
        Toast("修改失败" + data.msg, "error", 500, 1000, 100);
        // narn("error", "修改失败-" + data.msg, "提示");
      }
    }
  });

  // 取消浏览器自动填充的字符----------------
  // window.onload = function () { //太早
  // 取消软件填充的字符----------------
  setTimeout(function () {
    var p = $("input[name='oldPassWord'").val();
    // console.log(p);
    // $("input").val('')
    // $("input[name='oldPassWord'").val('')
    $("input").removeAttr("readonly");
  }, 100);
  // }
});

//封装一个Toast 组件 (提供重复利用)--------------------------------
function Toast(msg, type, time1, time2, bottom) {
  var bottom = bottom || 40;
  //msg 提示的内容 ,type 类型风格
  var type = type || "success"; //warning error
  var htmlStr = '<div class="toast  ' + type + '" style=" bottom: ' + bottom + " px" + '">' + msg + "</div>"; //插入body末尾处
  $(".toast").remove();
  $("body").append(htmlStr);
  setTimeout(function () {
    $(".toast").fadeOut(time2, function () {});
  }, time1);
}

//===========================================键盘监听事件======================================
$(document).keydown(function (event) {
  //   event.preventDefault();
  if (event.keyCode == 13) {
  } else if (event.keyCode == 27) {
    //ESC键盘
    $(".modal").hide(500);
    $(".modal-backdrop").hide(200);
    // $(".close").trigger("click");
    $("#loading").hide();
    window.parent.windowParnet();
  }
});

// ==================================表单规范验证=============================================
$(function () {
  $("#formSignin").validate({
    rules: {
      password: "required",
      userName: "required",
      oldName: "required",
      newName: {
        required: true,
        rangelength: [4, 12],
      },
      oldPassWord: "required",
      newPassWord: {
        required: true,
        rangelength: [6, 12],
      },
      newPassWord2: {
        required: true,
        rangelength: [6, 12],
      },
    },
    messages: {
      username: "请输入用户名",
      password: "不能为空",
      repassword: "请输入一样的",
      oldName: "不能为空",
      newName: {
        required: "中英文字符不超过20个",
        minLength: "密码最短不能少于4位数",
        maxLength: "密码最长不能超过12位",
        rangelength: "位数不对!",
      },

      oldPassWord: "不能为空",
      newPassWord: {
        required: "除特殊字符的20位以内中英文字符",
        minLength: "密码最短不能少于6位数",
        maxLength: "密码最长不能超过12位",
        rangelength: "位数不对!",
      },
      newPassWord2: {
        required: "不能为空，两次密码要一样",
        minLength: "密码最短不能少于6位数",
        rangelength: "位数不对!",
        maxLength: "密码最长不能超过12位",
      },
    },
    error: function (e) {
      console.log(e);
    },
  });
});
jQuery(document).ready(function () {
  jQuery("#formSignin").validate({
    submitHandler: function (form) {
      // your ajax loading logic
      form.submit(); // use this to finally submit form data at the last step
      return false; // prevent form submit because you are doing the ajax
    },
  });
});
